# -*- coding: UTF-8 -*-
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from scipy import optimize

# S型函数
def sigmoid(z):
    h = np.zeros((len(z), 1))  # 初始化，与z的长度一置
    h = 1.0 / (1.0 + np.exp(-z))
    return h

def costfunction(X,y,initial_theta,initial_lambda):
    m = len(y)
    J = 0

    h = sigmoid(np.dot(X, initial_theta))  # 计算h(z)
    # J = (-1/2*m)*np.sum(np.log(h)*y-(1-y)*np.log(h))
    J = (np.dot(np.transpose(y),np.log(h))-np.dot(np.transpose(1-y),np.log(1-h)))/-2*m

    return J


if __name__ == '__main__':
    load_breast_cancer = load_breast_cancer()
    data = load_breast_cancer.data
    target = load_breast_cancer.target

    X, X_test, y, y_test = train_test_split(data, target, test_size=0.2, random_state=42)

    m = len(y)
    X = np.hstack((np.ones((m, 1)), X))  # 在X前加一列1
    initial_theta = np.zeros((X.shape[1],1))#初始化theta
    initial_lambda = 0.1

    J = costfunction(X,y,initial_theta,initial_lambda)

    result = optimize.fmin_bfgs(costfunction, initial_theta, args=(X, y, initial_lambda))

    print(result)